/**
Copyright [2009] [Julian Gamble] 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 
	
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
 **/
package cruiseprojectlist.service;

import static cruiseprojectlist.service.HTMLFormattingService.blankLine;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import cruiseprojectlist.domain.ProjectPerformance;
import cruiseprojectlist.domain.ProjectPreference;
import cruiseprojectlist.domain.ProjectScores;
import cruiseprojectlist.domain.ProjectsStorageBucket;

public class ProjectPerformanceService {
	
	private Map<String,ProjectPerformance> projectPerformances = null;

	public ProjectPerformanceService() {
		initProjectPerformances();
	}
	
	private void initProjectPerformances() {
		projectPerformances = new TreeMap<String, ProjectPerformance>();
		ProjectListService projectListService = new ProjectListService();
		Set<String> projectList = projectListService.getProjectList();
		
		for (String projectName : projectList) {
			ProjectPerformance projectPerformance = new ProjectPerformance(projectName);
			projectPerformances.put(projectName,projectPerformance);
		}		
	}

	public ProjectPerformance getProjectPerformance(String projectName) {
		return projectPerformances.get(projectName);
	}

	public char[] getProjectListAsString() {
		Set<String> projects = projectPerformances.keySet();
		String result = "";
		result += getCoverageAsHTML(projects);
		result += HTMLFormattingService.blankLine();
		result += getComplexityAsHTML(projects);
		return result.toCharArray();
	}
	
	private String getComplexityAsHTML(Set<String> projects) {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		//String result = "<table>";
		String result = "";
		result += getComplexityHeadings(projectStorageBucket);
		/*result += headerRow(tableCell("Project Name")
				+tableCell(projectStorageBucket.getMinimumComplexityLabel())
				+tableCell(projectStorageBucket.getTargetComplexityLabel())
				+tableCell("Complexity Score")
				);*/
		for (String projectName : projects) {
			result = getProjectComplexityRow(result, projectName);
		}
		//result += "</table>";
		return result;
	}

	private String getComplexityHeadings(
			ProjectsStorageBucket projectStorageBucket) {
		String result = "";
		result += "  <!-- Heading - current complexity --> \n"
				+ "          <tr style='HEIGHT: 25.5pt' height='34'> \n"
				+ "            <td class='xl36' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 104pt; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 25.5pt; BACKGROUND-COLOR: navy' \n"
				+ "            width='139' height='34'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Release Kit</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong> \n"
				+ "            "+projectStorageBucket.getMinimumComplexityLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size='2'><strong> \n"
				+ "            "+projectStorageBucket.getTargetComplexityLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Current <br> \n"
				+ "            Complexity</strong></font></td> \n"
				+ "          </tr> \n"
				+ "         	<!-- Heading - current complexity --> \n" + " ";
		return result;
	}

	private String getProjectComplexityRow(String result, String projectName) {
		ProjectPerformance projectPerformance = projectPerformances.get(projectName);
		ProjectPreference projectPreference = projectPerformance.getProjectPreference();
		ProjectScores projectScore = projectPerformance.getProjectScores();
		/*result += tableRow(tableCell(projectName) 
				+ tableCell(projectPreference.getComplexityMinimum())
				+ tableCell(projectPreference.getComplexityTarget())
				+ tableCell(projectScore.getComplexityScore())
		);*/
		if (projectPreference.isVisible()) {
			result += getFormattedComplexityRow(projectName, projectPreference.getComplexityMinimum(),
					projectPreference.getComplexityTarget(),projectScore.getComplexityScore(),
					projectPerformance.getComplexityColour());
		}
		return result;
	}

	private String getFormattedComplexityRow(String projectName,
			double complexityMinimum, double complexityTarget,
			double complexityScore, String complexityColour) {
		String result = "";
		DecimalFormat df = new java.text.DecimalFormat("0.000");

		result += "<!-- Begin project "+projectName+" - Coverage --> \n"
				+ "          <tr style='HEIGHT: 12.75pt' height='17'> \n"
				+ "            <td class='xl25' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent' \n"
				+ "            height='17'><font face='Arial' size='2'>"+projectName+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl33' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            width='114' x:num='0.995'><font face='Arial' size= \n"
				+ "            '2'>"+complexityMinimum+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl27' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            x:num='0.99829999999999997'><font face='Arial' size= \n"
				+ "            '2'>"+complexityTarget+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl26' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BACKGROUND: "+complexityColour+"; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; mso-ignore: style; mso-pattern: auto none' \n"
				+ "            x:num='1'><font face='Arial' size='2'>"+df.format(complexityScore)+"</font></td> \n"
				+ " \n" 
				+ "          </tr> \n" + 
				"<!-- End project "+projectName+" - Coverage --> \n";
		
		return result;
	}

	private String getCoverageAsHTML(Set<String> projects) {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		String result = "";
		//result += "<table>";
		result += getCoverageHeadings(projectStorageBucket);
		/*result += headerRow(tableCell("Project Name")
				+tableCell(projectStorageBucket.getMinimumCoverageLabel())
				+tableCell(projectStorageBucket.getTargetCoverageLabel())
				+tableCell("Coverage Score")
				);*/
		for (String projectName : projects) {
			result = getProjectCoverageRow(result, projectName);
		}
		//result += "</table>";
		return result;
	}

	private String getCoverageHeadings(ProjectsStorageBucket projectStorageBucket) {
		String result = "";
		result += "  <!-- Heading - current coverage --> \n"
				+ "          <tr style='HEIGHT: 25.5pt' height='34'> \n"
				+ "            <td class='xl36' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 104pt; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 25.5pt; BACKGROUND-COLOR: navy' \n"
				+ "            width='139' height='34'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Release Kit</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong> \n"
				+ "            "+projectStorageBucket.getMinimumCoverageLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size='2'><strong> \n"
				+ "            "+projectStorageBucket.getTargetCoverageLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Current <br> \n"
				+ "            Coverage</strong></font></td> \n"
				+ "          </tr> \n"
				+ "         	<!-- Heading - current coverage --> \n" + " ";
		return result;
	}

	private String getProjectCoverageRow(String result, String projectName) {
		ProjectPerformance projectPerformance = projectPerformances.get(projectName);
		ProjectPreference projectPreference = projectPerformance.getProjectPreference();
		ProjectScores projectScore = projectPerformance.getProjectScores();
		/*result += tableRow(tableCell(projectName) 
				+ tableCell(projectPreference.getCoverageMinimum())
				+ tableCell(projectPreference.getCoverageTarget())
				+ tableCell(projectScore.getCoverageScore())
		);*/
		if (projectPreference.isVisible()) {
			result += getFormattedCoverageRow(projectName, projectPreference.getCoverageMinimum(),
					projectPreference.getCoverageTarget(),projectScore.getCoverageScore(),
					projectPerformance.getCoverageColour());
		}
		return result;
	}

	private String getFormattedCoverageRow(String projectName,
			double coverageMinimum, double coverageTarget, double coverageScore, String coverageColour) {
		String result = "";
		
		DecimalFormat df = new java.text.DecimalFormat("0.000");

		result += "<!-- Begin project "+projectName+" - Coverage --> \n"
				+ "          <tr style='HEIGHT: 12.75pt' height='17'> \n"
				+ "            <td class='xl25' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent' \n"
				+ "            height='17'><font face='Arial' size='2'>"+projectName+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl33' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            width='114' x:num='0.995'><font face='Arial' size= \n"
				+ "            '2'>"+coverageMinimum+"%</font></td> \n"
				+ " \n"
				+ "            <td class='xl27' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            x:num='0.99829999999999997'><font face='Arial' size= \n"
				+ "            '2'>"+coverageTarget+"%</font></td> \n"
				+ " \n"
				+ "            <td class='xl26' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BACKGROUND: "+coverageColour+"; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; mso-ignore: style; mso-pattern: auto none' \n"
				+ "            x:num='1'><font face='Arial' size='2'>"+df.format(coverageScore*100)+"%</font></td> \n"
				+ " \n" 
				+ "          </tr> \n" + 
				"<!-- End project "+projectName+" - Coverage --> \n";
		return result;
	}

	/*private String headerRow(String rowValue) {
		String result = "";
		result += "<th>";
		result += rowValue;
		result += "</th>";
		return result;
	}*/

	private String tableRow(String rowValue) {
		String result = "";
		result += "<tr>";
		result += rowValue;
		result += "</tr>";
		return result;
	}

	private String tableCell(Object cellValue) {
		String result = "";
		result += "<td>";
		result += cellValue;
		result += "</td>";
		return result;
	}

	public char[] getProjectPreferenceListAsString() {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		
		Set<String> projects = projectPerformances.keySet();
		String result = "";
		result += "<FORM ACTION='ViewPreferences' METHOD='POST'>";
		result += "<tr><td>Cruise Config File Loc: </td><td colspan='3'>";
		result += "<INPUT TYPE='text' VALUE='"+projectStorageBucket.getCruiseConfigPath()
			+"' name='cruiseConfigPath' size='80'></td></tr>";
		result += blankLine();
		result += getCoveragePreferencesAsHTML(projects);
		result += blankLine();
		result += getComplexityPreferencesAsHTML(projects);
		result += blankLine();
		result += "<tr><td><INPUT TYPE='SUBMIT' VALUE='Submit Preferences'></td></tr>";
		result += blankLine();
		result += getTitleFields();
		result += "</FORM>";
		return result.toCharArray();
	}

	private String getTitleFields() {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		String result = "";
		//result += "<table>";
		result += tableRow(
		tableCell("Minimum Coverage Label")
		 + tableCell("<INPUT TYPE='text' VALUE='"+projectStorageBucket.getMinimumCoverageLabel()
			+"' name='coverageMinimumTitle'>"));

		result += tableRow(
				tableCell("Target Coverage Label")
				 + tableCell("<INPUT TYPE='text' VALUE='"+projectStorageBucket.getTargetCoverageLabel()
			+"' name='coverageTargetTitle'>"));
		
		result += tableRow(
				tableCell("Minimum Complexity Label")
				 + tableCell( "<INPUT TYPE='text' VALUE='"+projectStorageBucket.getMinimumComplexityLabel()
			+"' name='complexityMinimumTitle'>"));
		
		result += tableRow(
				tableCell("Target Complexity Label")
				 + tableCell("<INPUT TYPE='text' VALUE='"+projectStorageBucket.getTargetComplexityLabel()
			+"' name='complexityTargetTitle'>"));

		//result += "</table>";
		
		return result;
	}

	private String getCoveragePreferencesAsHTML(Set<String> projects) {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		String result = "";
		//result += "<table>";
		result += getCoveragePreferenceHeadings(projectStorageBucket);
		/*result += headerRow(tableCell("Project Name")
				+tableCell(projectStorageBucket.getMinimumCoverageLabel())
				+tableCell(projectStorageBucket.getTargetCoverageLabel())
				+tableCell("Cobertura File Path")
				);*/
		for (String projectName : projects) {
			result += getProjectPreferencesCoverageRow( projectName);
		}
		//result += "</table>";
		return result;
	}
	
	private String getCoveragePreferenceHeadings(
			ProjectsStorageBucket projectStorageBucket) {
		String result = "";
		result += "  <!-- Heading - current coverage --> \n"
				+ "          <tr style='HEIGHT: 25.5pt' height='34'> \n"
				+ "            <td class='xl36' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 104pt; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 25.5pt; BACKGROUND-COLOR: navy' \n"
				+ "            width='139' height='34'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Release Kit</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong> \n"
				+ "            "+projectStorageBucket.getMinimumCoverageLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size='2'><strong> \n"
				+ "            "+projectStorageBucket.getTargetCoverageLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='150'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>  \n"
				+ "            Project Visible</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='150'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>  \n"
				+ "            Cobertura File Path</strong></font></td> \n"
				+ "          </tr> \n"
				+ "         	<!-- Heading - current coverage --> \n" + " ";
		return result;
	}

	private String getComplexityPreferencesAsHTML(Set<String> projects) {
		ProjectsStorageBucket projectStorageBucket = ProjectStorageBucketService.getProjectsStorageBucket();
		String result = "";
		//result += "<table>";
		result += getComplexityPreferenceHeadings(projectStorageBucket);
		/*result += headerRow(tableCell("Project Name")
				+tableCell(projectStorageBucket.getMinimumComplexityLabel())
				+tableCell(projectStorageBucket.getTargetComplexityLabel())
				);*/
		for (String projectName : projects) {
			result += getProjectPreferencesComplexityRow( projectName);
		}
		//result += "</table>";
		return result;
	}

	private String getComplexityPreferenceHeadings(
			ProjectsStorageBucket projectStorageBucket) {
		String result = "";
		result += "  <!-- Heading - current complexity --> \n"
				+ "          <tr style='HEIGHT: 25.5pt' height='34'> \n"
				+ "            <td class='xl36' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; WIDTH: 104pt; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 25.5pt; BACKGROUND-COLOR: navy' \n"
				+ "            width='139' height='34'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Release Kit</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong> \n"
				+ "            "+projectStorageBucket.getMinimumComplexityLabel()+"</strong></font></td> \n"
				+ " \n"
				+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size='2'><strong> \n"
				+ "            "+projectStorageBucket.getTargetComplexityLabel()+"</strong></font></td> \n"
				+ " \n"
				/*+ "            <td class='xl37' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: navy' \n"
				+ "            width='114'><font face='Arial' color='#FFFFFF' size= \n"
				+ "            '2'><strong>Current <br> \n"
				+ "            Coverage</strong></font></td> \n"*/
				+ "          </tr> \n"
				+ "         	<!-- End Heading - current complexity --> \n" + " ";
		return result;
	}

	private String getProjectPreferencesCoverageRow(
			String projectName) {
		String result = "";
		ProjectPerformance projectPerformance = projectPerformances.get(projectName);
		ProjectPreference projectPreference = projectPerformance.getProjectPreference();
		/*ProjectScores projectScore = projectPerformance.getProjectScores();*/
		/*result += tableRow(tableCell(projectName) 
				+ tableCellWithField(projectName+"coverageMinimum",projectPreference.getCoverageMinimum())
				+ tableCellWithField(projectName+"coverageTarget",projectPreference.getCoverageTarget())
				+ tableCellWithFieldWithSize(projectName+"coberturaPath",projectPreference.getCoberturaFilePath())
		);*/
		result += getFormattedCoveragePreferenceRow(projectName, 
				field(projectName+"coverageMinimum",projectPreference.getCoverageMinimum()),
				field(projectName+"coverageTarget",projectPreference.getCoverageTarget()),
				checkbox(projectName+"isVisible",projectPreference.isVisible()),
				fieldWithSize(projectName+"coberturaPath",projectPreference.getCoberturaFilePath())
				);
		return result;
	}

	private String checkbox(String fieldName, boolean visible) {
		String result = "";
		result += "<input type='checkbox' name='"+fieldName+"' " 
		+ (visible ? "checked ": "")
		+"/>";
		return result;
	}

	private String getFormattedCoveragePreferenceRow(String projectName,
			String coverageMinimum, String coverageTarget,
			String isVisible, String coberturaPath) {
		String result = "";

		result += "<!-- Begin project "+projectName+" - Coverage --> \n"
				+ "          <tr style='HEIGHT: 12.75pt' height='17'> \n"
				+ "            <td class='xl25' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent' \n"
				+ "            height='17'><font face='Arial' size='2'>"+projectName+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl33' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            width='114' x:num='0.995'><font face='Arial' size= \n"
				+ "            '2'>"+coverageMinimum+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl27' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            x:num='0.99829999999999997'><font face='Arial' size= \n"
				+ "            '2'>"+coverageTarget+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl26' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext;  BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; mso-ignore: style; mso-pattern: auto none' \n"
				+ "            x:num='1'><font face='Arial' size='2'>"+isVisible+"</font></td> \n"
				+ " \n" 
				+ "            <td class='xl26' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext;  BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; mso-ignore: style; mso-pattern: auto none' \n"
				+ "            x:num='1'><font face='Arial' size='2'>"+coberturaPath+"</font></td> \n"
				+ " \n" 				
				+ "          </tr> \n" + 
				"<!-- End project "+projectName+" - Coverage --> \n";
		return result;
	}

	private String getProjectPreferencesComplexityRow(String projectName) {
		String result = "";
		ProjectPerformance projectPerformance = projectPerformances.get(projectName);
		ProjectPreference projectPreference = projectPerformance.getProjectPreference();
		/*result += tableRow(tableCell(projectName) 
				+ tableCellWithField(projectName+"complexityMinimum",projectPreference.getComplexityMinimum())
				+ tableCellWithField(projectName+"complexityTarget",projectPreference.getComplexityTarget())
		);*/
		result += getFormattedComplexityPreferencesRow(projectName, 
				field(projectName+"complexityMinimum",projectPreference.getComplexityMinimum()),
				field(projectName+"complexityTarget",projectPreference.getComplexityTarget())
				);
		return result;
	}

	private String getFormattedComplexityPreferencesRow(String projectName,
			String complexityMinimum, String complexityTarget) {
		String result = "";

		result += "<!-- Begin project "+projectName+" - Complexity --> \n"
				+ "          <tr style='HEIGHT: 12.75pt' height='17'> \n"
				+ "            <td class='xl25' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; HEIGHT: 12.75pt; BACKGROUND-COLOR: transparent' \n"
				+ "            height='17'><font face='Arial' size='2'>"+projectName+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl33' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; WIDTH: 86pt; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            width='114' x:num='0.995'><font face='Arial' size= \n"
				+ "            '2'>"+complexityMinimum+"</font></td> \n"
				+ " \n"
				+ "            <td class='xl27' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BORDER-LEFT: windowtext; BORDER-BOTTOM: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent' \n"
				+ "            x:num='0.99829999999999997'><font face='Arial' size= \n"
				+ "            '2'>"+complexityTarget+"</font></td> \n"
				+ " \n"
/*				+ "            <td class='xl26' style= \n"
				+ "            'BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext; BACKGROUND: "+complexityColour+"; BORDER-LEFT: windowtext 0.5pt solid; BORDER-BOTTOM: windowtext 0.5pt solid; mso-ignore: style; mso-pattern: auto none' \n"
				+ "            x:num='1'><font face='Arial' size='2'>"+complexityScore+"</font></td> \n"
*/				+ " \n" 
				+ "          </tr> \n" + 
				"<!-- End project "+projectName+" - Complexity --> \n";
		
		return result;
	}

	/*private String tableCellWithField(String fieldName, Object cellValue) {
		String result = "";
		result += "<td>";
		result += "<input type='text' name='"+fieldName+"' value='"+cellValue+"'/>";
		result += "</td>";
		return result;
	}*/
	
	private String field(String fieldName, Object cellValue) {
		String result = "";
		result += "<input type='text' name='"+fieldName+"' value='"+cellValue+"'/>";
		return result;
	}
	
	/*private String tableCellWithFieldWithSize(String fieldName,
			Object cellValue) {
		String result = "";
		result += "<td>";
		result += "<input type='text' name='"+fieldName+"' value='"+cellValue+"' size='80'/>";
		result += "</td>";
		return result;
	}*/
	
	private String fieldWithSize(String fieldName,
			Object cellValue) {
		String result = "";
		result += "<input type='text' name='"+fieldName+"' value='"+cellValue+"' size='80'/>";
		return result;
	}

	public void saveProjectPreferences(Map<String,String[]> parameterMap) {
		Set<String> projects = projectPerformances.keySet();
		for (String projectName : projects) {
			ProjectPreference projectPreference = new ProjectPreference();

			setCoberturaFilePath(parameterMap, projectName, projectPreference);
			setIsVisible(parameterMap, projectName, projectPreference);
			setCoverageMinimum(parameterMap, projectName, projectPreference);
			setCoverageTarget(parameterMap, projectName, projectPreference);
			setComplexityMinimum(parameterMap, projectName, projectPreference);
			setComplexityTarget(parameterMap, projectName, projectPreference);
			ProjectPerformance projectPerformance =  projectPerformances.get(projectName);
			projectPerformance.setProjectPreference(projectPreference);
		}
		saveProjectPrefences(parameterMap);
	}

	private void setIsVisible(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] isVisible = parameterMap.get(projectName+"isVisible");
		if (null!=isVisible)
			projectPreference.setVisible(isVisible);
		else
			projectPreference.setVisible(false);
	}

	private void saveProjectPrefences(Map<String,String[]> parameterMap) {
		ProjectsStorageBucket projectsStorageBucket = new ProjectsStorageBucket();
		Map<String,ProjectPreference> projectPreferences = new TreeMap<String,ProjectPreference>();
		for (String projectName : projectPerformances.keySet()) {
			ProjectPerformance projectPerformance = projectPerformances.get(projectName);
			ProjectPreference projectPreference = projectPerformance.getProjectPreference();
			projectPreferences.put(projectName,projectPreference);
		}
		projectsStorageBucket.setProjectPreferences(projectPreferences);
		setCruiseConfigPath(parameterMap, projectsStorageBucket);
		
		setMinimumCoverageLabel(parameterMap, projectsStorageBucket);
		setTargetCoverageLabel(parameterMap, projectsStorageBucket);
		
		setMinimumComplexityLabel(parameterMap, projectsStorageBucket);
		setTargetComplexityLabel(parameterMap, projectsStorageBucket);
		
		ProjectStorageBucketService.save(projectsStorageBucket);
	}

	private void setTargetComplexityLabel(Map<String, String[]> parameterMap,
			ProjectsStorageBucket projectsStorageBucket) {
		String[] coverageTargetTitle = parameterMap.get("complexityTargetTitle");
		if (null!=coverageTargetTitle)
			projectsStorageBucket.setProjectTargetComplexityLabel(coverageTargetTitle[0]);
	}

	private void setMinimumComplexityLabel(Map<String, String[]> parameterMap,
			ProjectsStorageBucket projectsStorageBucket) {
		String[] coverageTargetTitle = parameterMap.get("complexityMinimumTitle");
		if (null!=coverageTargetTitle)
			projectsStorageBucket.setProjectMinimumComplexityLabel(coverageTargetTitle[0]);
	}

	private void setTargetCoverageLabel(Map<String, String[]> parameterMap,
			ProjectsStorageBucket projectsStorageBucket) {
		String[] coverageTargetTitle = parameterMap.get("coverageTargetTitle");
		if (null!=coverageTargetTitle)
			projectsStorageBucket.setProjectTargetCoverageLabel(coverageTargetTitle[0]);
	}

	private void setMinimumCoverageLabel(Map<String, String[]> parameterMap,
			ProjectsStorageBucket projectsStorageBucket) {
		String[] coverageMinimumTitle = parameterMap.get("coverageMinimumTitle");
		if (null!=coverageMinimumTitle)
			projectsStorageBucket.setProjectMinimumCoverageLabel(coverageMinimumTitle[0]);
	}

	private void setCruiseConfigPath(Map<String, String[]> parameterMap,
			ProjectsStorageBucket projectsStorageBucket) {
		String[] cruiseConfigPath = parameterMap.get("cruiseConfigPath");
		if (null!=cruiseConfigPath)
			projectsStorageBucket.setCruiseConfigPath(cruiseConfigPath[0]);
	}

	private void setComplexityTarget(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] complexityTargetParam = parameterMap.get(projectName+"complexityTarget");
		if (null!=complexityTargetParam){
			double complexityTarget = Double.valueOf(complexityTargetParam[0]);
			projectPreference.setComplexityTarget(complexityTarget);
		}
	}

	private void setComplexityMinimum(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] complexityMinimumParam = parameterMap.get(projectName+"complexityMinimum");
		if (null!=complexityMinimumParam){
			double complexityMinimum = Double.valueOf(complexityMinimumParam[0]);
			projectPreference.setComplexityMinimum(complexityMinimum);
		}
	}

	private void setCoverageTarget(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] coverageTargetParam = parameterMap.get(projectName+"coverageTarget");
		if (null!=coverageTargetParam){
			double coverageTarget = Double.valueOf(coverageTargetParam[0]);
			projectPreference.setCoverageTarget(coverageTarget);
		}
	}

	private void setCoverageMinimum(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] coverageMinimumParam = parameterMap.get(projectName+"coverageMinimum");
		if (null!=coverageMinimumParam){
			double coverageMinimum = Double.valueOf(coverageMinimumParam[0]);
			projectPreference.setCoverageMinimum(coverageMinimum);
		}
	}

	private void setCoberturaFilePath(Map<String, String[]> parameterMap,
			String projectName, ProjectPreference projectPreference) {
		String[] coberturaFilePath = parameterMap.get(projectName+"coberturaPath");
		if (null!=coberturaFilePath)
			projectPreference.setCoberturaFilePath(coberturaFilePath[0]);
	}

}
